package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.gsk.europepharma.salesReport.utils.UtilsSalesReport;

public class UpdateLevelInformations extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		// Variables de session
		HttpSession sessionObject = request.getSession();
		Integer cs_adm_pdt = (Integer) sessionObject.getAttribute("cs_adm_pdt");
		Integer cs_adm_rgpt = (Integer) sessionObject.getAttribute("cs_adm_rgpt");
		Integer cs_adm_prstn = (Integer) sessionObject.getAttribute("cs_adm_prstn");
		Integer cs_network = (Integer) sessionObject.getAttribute("cs_network");
		Integer cs_month = (Integer) sessionObject.getAttribute("cs_month");
		// Fin de session
		
		// Recuperation des parametres
		String source = request.getParameter("source");
		Integer CS_SRC = Integer.parseInt(source);
		String unite = request.getParameter("unite");
		String periode = request.getParameter("periode");
		String idGeog = request.getParameter("idGeog");
		String nivGeog = request.getParameter("nivGeog");
		// Fin de recuperation des parametres
				
		// Requete
		tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
		Session session = salesDAO.getSession();
		String strReq = salesDAO.QUERY_RECUP_SALES;
		Query req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
		req.setString("data", unite + periode);	
		req.setInteger("cs_adm_pdt", cs_adm_pdt);
		req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
		req.setInteger("cs_adm_prstn", cs_adm_prstn);
		req.setInteger("CS_SRC", CS_SRC);
		req.setInteger("cs_network", cs_network);
		req.setInteger("cs_month", cs_month);
		req.setString("nivGeog", nivGeog); 
		req.setInteger("idGeog", Integer.parseInt(idGeog));
		req.setMaxResults(10);
		ArrayList result = new ArrayList(req.list());
		Iterator iter = result.iterator();
		String name = "";
		String pm = "";
		String deltapm = "";
		String vol = "";
		String deltavol = "";
		String evol = "";
		while (iter.hasNext()) {
			Object[] val = (Object[]) iter.next();
			name = val[0].toString();
			deltapm = UtilsSalesReport.getDeltaPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), Double.parseDouble(val[3].toString()), Double.parseDouble(val[4].toString()), 1) + "";
			pm = UtilsSalesReport.getPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[3].toString()), 1) + "";
			vol = UtilsSalesReport.getVol(Double.parseDouble(val[1].toString()), 0) + "";
			deltavol = UtilsSalesReport.getDeltaVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 0) + "";
			evol = UtilsSalesReport.getEvolVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 1) + "";
		}
		// Fin requete
		
		// Envoie JSP
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("name", name);
		hm.put("pm", pm);
		hm.put("deltapm", deltapm);
		hm.put("vol", vol);
		hm.put("deltavol", deltavol);
		hm.put("evol", evol);
		JSONObject json = JSONObject.fromObject(hm);
		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();
        // Fin envoie JSP 
		
		return null;
	}

}
